package cn.gyouzhe.mapper;

import cn.gyouzhe.entity.Coupon;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 券mapper
 *
 * @author wangchuan
 * @datetime 2018/2/28 23:16
 */
@Mapper
public interface CouponMapper {

    /**
     * 查询券信息
     * @param couponId 券ID
     * @return 券信息
     */
    @Select("select id,good_id,coupon_id,price,demand,start_time,end_time from g_coupon where coupon_id=#{couponId}")
    @Results({
            @Result(property = "id", column = "id", id = true),
            @Result(property = "goodId", column = "good_id"),
            @Result(property = "couponId", column = "coupon_id"),
            @Result(property = "price", column = "price"),
            @Result(property = "condition", column = "demand"),
            @Result(property = "startTime", column = "start_time"),
            @Result(property = "endTime", column = "end_time"),
    })
    Coupon selectCoupon(@Param("couponId") String couponId);

    /**
     * 保存券信息
     * @param coupon 券信息
     */
    @Insert("insert into g_coupon(good_id,coupon_id,price,demand,start_time,end_time) values(#{goodId},#{couponId},#{price},#{condition},#{startTime},#{endTime})")
    @Options(useGeneratedKeys = true)
    void saveCoupon(Coupon coupon);

    /**
     * 查询已经过期的券信息
     * @param expireTime 过期时间
     * @return 券列表
     */
    @Select("select id,good_id,coupon_id,price,demand,start_time,end_time from g_coupon where end_time <=#{expireTime}")
    @Results({
            @Result(property = "id", column = "id", id = true),
            @Result(property = "goodId", column = "good_id"),
            @Result(property = "couponId", column = "coupon_id"),
            @Result(property = "price", column = "price"),
            @Result(property = "condition", column = "demand"),
            @Result(property = "startTime", column = "start_time"),
            @Result(property = "endTime", column = "end_time"),
    })
    List<Coupon> selectExpireCoupon(@Param("expireTime") LocalDateTime expireTime);

    /**
     * 删除券信息
     * @param id 券数据库ID
     */
    @Delete("delete from g_coupon where id=#{id}")
    void deleteCoupon(@Param("id") int id);
}
